package com.changpay.zywy.ui.activity.serivce

import android.content.Intent
import android.text.TextUtils
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.activity.mine.LocationSelectMoreActivity
import kotlinx.android.synthetic.main.activity_jie_suan_ka_change.*
import kotlinx.android.synthetic.main.title_layout.*
import java.util.*
import kale.ui.view.dialog.EasyDialog


class JieSuanKaChangeActivity : BaseActivity(), IView {

    var settleAccountType: String = ""
    var proviceId = ""
    var cityId = ""
    var areaId = ""
    var proviceName = ""
    var cityName = ""
    var areaName = ""
    private var bank_code: String = ""
    private var alliedBankCode: String = ""


    override fun attachLayoutRes(): Int {
        return R.layout.activity_jie_suan_ka_change
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    override fun initData() {
    }

    override fun initView() {
        tv_title.text = "结算卡修改"
        iv_back.setOnClickListener { finish() }
        btn_apply.setOnClickListener { submit() }
        tv_detail_address.setOnClickListener {
            Intent(this, LocationSelectMoreActivity::class.java).run {
                startActivityForResult(this, 100)
            }
        }

        tv_shop_code.setOnClickListener {
            Intent(this, SelectShopActivity::class.java).run {
                startActivityForResult(this, 200)
            }
        }
        val accountType: Array<String> = arrayOf("对公", "对私")

        tv_account_type.setOnClickListener {
            val dialog = EasyDialog.builder(this)
                .setTitle("账号类型")
                // 这里传入的“1”表示默认选择第二个选项
                .setSingleChoiceItems(
                    accountType, -1
                ) { d, position ->
                    when (position) {
                    0 -> {
                        tv_account_type.text = "对公"
                        settleAccountType = "OPEN"
                    }
                    1 -> {
                        tv_account_type.text = "对私"
                        settleAccountType = "INDIVIDUAL"
                    }
                }
                d.dismiss()
                }
                .build<EasyDialog>()
            dialog.show(supportFragmentManager)
        }

        tv_detail_name.setOnClickListener {
            if (proviceId.isEmpty()) {
                showToast("请先选择开户地区")
                return@setOnClickListener
            }
            val bankname = et_new_bank_name.text.toString()
            if (bankname.isEmpty()) {
                showToast("请输入新开户银行")
                return@setOnClickListener
            }
            Intent(this, SelectBankActivity::class.java).run {
                putExtra("provinceId", proviceId)
                putExtra("cityId", cityId)
                putExtra("areaId", areaId)
                putExtra("bank_name", bankname)
                startActivityForResult(this, 300)
            }
        }
    }


    /**
     * 点击注册按钮
     */
    private fun submit() {

        val code = tv_shop_code.text.toString()
        if (TextUtils.isEmpty(code)) {
            showToast("请选择商户编号")
            return
        }
        val pwd = et_new_card_number.text.toString()
        if (TextUtils.isEmpty(pwd)) {
            showToast("请输入新银行卡号")
            return
        }

        val bankAccountName = et_person_name.text.toString()
        if (TextUtils.isEmpty(pwd)) {
            showToast("请输入开户名称")
            return
        }

        val confirmPwd = et_new_bank_name.text.toString()
        if (TextUtils.isEmpty(confirmPwd)) {
            showToast("请输入新开户银行")
            return
        }

        val DetailName = tv_detail_name.text.toString()
        if (TextUtils.isEmpty(DetailName)) {
            showToast("请选择具体开户支行")
            return
        }
        val mPhone = et_phone.text.toString()
        if (TextUtils.isEmpty(mPhone)) {
            showToast("请输入手机号码")
            return
        }

        if (mPhone.length != 11) {
            showToast("请输入11位手机号码")
            return
        }
        if (TextUtils.isEmpty(settleAccountType)) {
            showToast("请先选择账户类型")
            return
        }
        val params = HashMap<String, String>()
        params["merchant_id"] = code
        params["bankAccountName"] = bankAccountName
        params["card_id"] = pwd
        params["deposit_bank"] = confirmPwd
        params["bank_branch"] = DetailName
        params["phone_num"] = mPhone
        params["settleAccountType"] = settleAccountType
        params["province"] = proviceId
        params["city"] = cityId
        params["bankCode"] = bank_code
        params["alliedBankCode"] = alliedBankCode
        params["key"] = token
        params["gc_id"] = intent.getStringExtra("gc_id")
        params["client"] = "android"
        RetrofitHelper.service.balance_change(params).sss(this)
        {
            showToast(it.result.toString())
            finish()
        }
    }

    override fun start() {

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (resultCode) {
            200 -> {
                tv_detail_address.text = data?.getStringExtra("provinceName").toString() + " " +
                        data?.getStringExtra("cityName").toString() + " " +
                        data?.getStringExtra("areaName").toString()
                proviceName = data?.getStringExtra("provinceName").toString()
                cityName = data?.getStringExtra("cityName").toString()
                areaName = data?.getStringExtra("areaName").toString()
                proviceId = data?.getStringExtra("provinceId").toString()
                cityId = data?.getStringExtra("cityId").toString()
                areaId = data?.getStringExtra("areaId").toString()

            }
            300 -> {
                tv_shop_code.text = data?.getStringExtra("shop_code").toString()
                tv_shop_name.text = data?.getStringExtra("shop_name").toString()
            }
            400 -> {
                tv_detail_name.text = data?.getStringExtra("bank_name").toString()
                bank_code = data?.getStringExtra("bank_code").toString()
                alliedBankCode = data?.getStringExtra("alliedBankCode").toString()
            }
        }
    }
}
